﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Properties;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class DateFieldLW : TextFieldLW
    {
        public DateFieldLW()
            : this( null )
        {
        }

        public DateFieldLW( IContainer components )
            : base( components )
        {
            base.Xtype = "datefield";
            base.Width = 96;
            //base.Text = DateTime.Today.ToShortDateString();
            Margin = new Padding( 0 );
        }

        [DefaultValue( 96 )]
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        /// <summary>
        ///   The default date format string which can be overriden for localization support.  The format must be
        ///   valid according to {@link Date#parseDate} (defaults to <tt>'m/d/Y'</tt>).
        /// </summary>
        [DefaultValue( ( string ) null )]
        [JsonProperty( "format", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The default date format string which can be overriden for localization support. The format must be valid according to Date.parseDate (defaults to 'm/d/Y')." )]
        public string Format { get; set; }

        /// <summary>
        ///   Multiple date formats separated by "<tt>|</tt>" to try when parsing a user input value and it
        ///   does not match the defined format (defaults to
        ///   <tt>'m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j'</tt>).
        /// </summary>
        [JsonProperty( "altFormats", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( " Multiple date formats separated by '|' to try when parsing a user input value and it does not match the defined format." )]
        public string AltFormats { get; set; }

        /// <summary>
        ///   The tooltip to display when the date falls on a disabled day (defaults to <tt>'Disabled'</tt>)
        /// </summary>
        [JsonProperty( "disabledDaysText", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The tooltip to display when the date falls on a disabled day" )]
        public string DisabledDaysText { get; set; }

        /// <summary>
        ///   The tooltip text to display when the date falls on a disabled date (defaults to <tt>'Disabled'</tt>)
        /// </summary>
        [JsonProperty( "disabledDatesText", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The tooltip text to display when the date falls on a disabled date" )]
        public string DisabledDatesText { get; set; }

        /// <summary>
        ///   The error text to display when the date in the cell is before <tt>{@link #minValue}</tt> (defaults to
        ///   <tt>'The date in this field must be after {minValue}'</tt>).
        /// </summary>
        [JsonProperty( "minText", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The error text to display when the date in the cell is before minValue." )]
        public string MinText { get; set; }

        /// <summary>
        ///   The error text to display when the date in the cell is after <tt>{@link #maxValue}</tt> (defaults to
        ///   <tt>'The date in this field must be before {maxValue}'</tt>).
        /// </summary>
        [JsonProperty( "maxText", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The error text to display when the date in the cell is after maxValue" )]
        public string MaxText { get; set; }

        /// <summary>
        ///   The error text to display when the date in the field is invalid (defaults to
        ///   <tt>'{value} is not a valid date - it must be in the format {format}'</tt>).
        /// </summary>
        [JsonProperty( "invalidText", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "The error text to display when the date in the field is invalid" )]
        public override string InvalidText { get; set; }

        /// <summary>
        ///   An additional CSS class used to style the trigger button.  The trigger will always get the
        ///   class <tt>'x-form-trigger'</tt> and <tt>triggerClass</tt> will be <b>appended</b> if specified
        ///   (defaults to <tt>'x-form-date-trigger'</tt> which displays a calendar icon).
        /// </summary>
        [JsonProperty( "triggerClass", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "An additional CSS class used to style the trigger button." )]
        public string TriggerClass { get; set; }

        /// <summary>
        ///   <tt>false</tt> to hide the footer area of the DatePicker containing the Today button and disable
        ///   the keyboard handler for spacebar that selects the current date (defaults to <tt>true</tt>).
        /// </summary>
        [JsonProperty( "showToday", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "false to hide the footer area of the DatePicker containing the Today button and disable the keyboard handler for spacebar that selects the current date" )]
        public bool ShowToday { get; set; }

        /// <summary>
        ///   Day index at which the week should begin, 0-based (defaults to 0, which is Sunday)
        /// </summary>
        [JsonProperty( "startDay", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "Day index at which the week should begin, 0-based." )]
        [DefaultValue( 0 )]
        public int StartDay { get; set; }

        /// <summary>
        ///   The minimum allowed date. Can be either a Javascript date object or a string date in a
        ///   valid format (defaults to null).
        /// </summary>
        [JsonProperty( "minValue", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [TypeConverter( typeof( StringConverter ) )]
        [Description( "The minimum allowed date." )]
        public object MinValue { get; set; }

        /// <summary>
        ///   The maximum allowed date. Can be either a Javascript date object or a string date in a
        ///   valid format (defaults to null).
        /// </summary>
        [JsonProperty( "maxValue", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [TypeConverter( typeof( StringConverter ) )]
        [Description( "The maximum allowed date." )]
        public object MaxValue { get; set; }

        /// <summary>
        ///   An array of days to disable, 0 based (defaults to null). Some examples:<pre><code>
        ///                                                                                 // disable Sunday and Saturday:
        ///                                                                                 disabledDays:  [0, 6]
        ///                                                                                 // disable weekdays:
        ///                                                                                 disabledDays: [1,2,3,4,5]
        ///                                                                               </code></pre>
        /// </summary>
        [JsonProperty( "disabledDays", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [TypeConverter( typeof( StringConverter ) )]
        [Description( "An array of days to disable, 0 based." )]
        public object DisabledDays { get; set; }

        /// <summary>
        ///   An array of "dates" to disable, as strings. These strings will be used to build a dynamic regular
        ///   expression so they are very powerful. Some examples:<pre><code>
        ///                                                              // disable these exact dates:
        ///                                                              disabledDates: ["03/08/2003", "09/16/2003"]
        ///                                                              // disable these days for every year:
        ///                                                              disabledDates: ["03/08", "09/16"]
        ///                                                              // only match the beginning (useful if you are using short years):
        ///                                                              disabledDates: ["^03/08"]
        ///                                                              // disable every day in March 2006:
        ///                                                              disabledDates: ["03/../2006"]
        ///                                                              // disable every day in every March:
        ///                                                              disabledDates: ["^03"]
        ///                                                            </code></pre>
        ///   Note that the format of the dates included in the array should exactly match the {@link #format} config.
        ///   In order to support regular expressions, if you are using a {@link #format date format} that has "." in
        ///   it, you will have to escape the dot when restricting dates. For example: <tt>["03\\.08\\.03"]</tt>.
        /// </summary>
        [JsonProperty( "disabledDates", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [TypeConverter( typeof( StringConverter ) )]
        [Description( "An array of 'dates' to disable, as strings." )]
        public object DisabledDates { get; set; }

        /// <summary>
        ///   A {@link Ext.DomHelper DomHelper element specification object}, or <tt>true</tt> for the default element
        ///   specification object:<pre><code>
        ///                               autoCreate: {tag: "input", type: "text", size: "10", autocomplete: "off"}
        ///                             </code></pre>
        /// </summary>
        [JsonProperty( "autoCreate", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [TypeConverter( typeof( StringConverter ) )]
        [Description( "A Ext.DomHelper DomHelper element specification object, or true for the default element specification object." )]
        public object AutoCreate { get; set; }

        protected override void OnPaint( PaintEventArgs e )
        {
            base.OnPaint( e );
            e.Graphics.DrawImage( Resources.Trigger_date, ClientSize.Width - 17, 0, 17, 22 );
            //e.Graphics.FillRectangle( new SolidBrush( Color.White ), ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 2, ClientRectangle.Height - 1 );
            e.Graphics.DrawRectangle( new Pen( new SolidBrush( Color.FromArgb( 181, 184, 200 ) ) ), ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 18, ClientRectangle.Height - 1 );
            e.Graphics.DrawLine( new Pen( new SolidBrush( Color.FromArgb( 181, 184, 200 ) ) ), ClientRectangle.Width - 18, ClientRectangle.Height - 1, ClientRectangle.Width, ClientRectangle.Height - 1 );

            //e.Graphics.DrawRectangle( new Pen( new SolidBrush( Color.FromArgb( 126, 173, 217 ) ) ), e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 18, e.ClipRectangle.Height - 1 );
            //e.Graphics.DrawImage( Properties.Resources.Trigger_date, e.ClipRectangle.Width - 17, e.ClipRectangle.Y, 17, 22 );
        }
    }
}
